<template>
  <div class="container">
    <div class="addedLinkage">
      <el-button size="mini" icon="el-icon-back" @click="$router.replace('/iot_rule')">返回</el-button>
      <div class="titleTr">
        <div class="inputBox">
          <div class="label">策略名称：</div>
          <el-input class="inputWidth" placeholder="请输入策略名称" v-model="dataInfo.name" size="small" />
          <div class="label" style="margin-left:30px;">备注：</div>
          <el-input class="inputWidth" placeholder="请输入备注（无备注则填写“无”）" size="small" v-model="dataInfo.description" />
          <div class="label" style="margin-left:30px;">所属组织：</div>
          <el-cascader size="small" ref="cascader" v-model="option1" :options="zuzhiTree" :props="defaultProps2"
            @change="zuzhiChange" clearable>
          </el-cascader>
          <div class="label" style="margin-left:30px;">所属项目：</div>
          <el-select size="small" v-model="xqData" value-key="id" placeholder="请选择项目" @change="checkedXq">
            <el-option v-for="item in xqTree" :label="item.name" :value="item" :key="item.id"></el-option>
          </el-select>
        </div>
        <div class="btnGroup">
          <el-button @click="reset" icon="el-icon-refresh-right">重置</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="saveAdd">确定添加</el-button>
        </div>
      </div>

      <!-- 触发条件 -->
      <div class="triggerCondition" v-if="isClickTrigger">
        <div class="text">还未添加触发条件</div>
        <div class="btn">
          <el-button size="small" type="info" icon="el-icon-plus" @click="isClickTrigger = false">添加触发条件</el-button>
        </div>
      </div>
      <div class="itemMaxBox" v-else>
        <div class="titleBox">
          <span>触发条件</span>
          <img class="icon" src="~@/assets/img/wenhao.png" />
        </div>

        <div class="itemBox" v-for="(item, index) in dataInfo.triggers" :key="item.idx">
          <div class="titleBox">
            <div class="titleText">触发器{{index+1}}</div>
            <el-button type="text" @click="deleteItem(index, 'dataInfo.triggers')">删除</el-button>
          </div>
          <div class="selectBox">
            <el-select v-if="routequery.modelType == 'scene_mode'" class="inputWidth" size="small"
              v-model="item.trigger" placeholder="请选择触发器" >
              <el-option v-for="ops in triggerOptionsScene" :key="ops.value" :label="ops.label" :value="ops.value">
              </el-option>
            </el-select>
            <el-select v-if="routequery.modelType == 'linkage_mode'" class="inputWidth" size="small"
              v-model="item.trigger" placeholder="请选择触发器" >
              <el-option v-for="ops in triggerOptionsLinkage" :key="ops.value" :label="ops.label" :value="ops.value">
              </el-option>
            </el-select>

            <div class="debounce" v-if="item.trigger === 'device'">
              <span class="text">防抖：</span>
              <el-switch active-value="true"
              inactive-value="false" @change="" v-model="item.device.shakeLimit.enabled" />
            </div>
          </div>

          <!-- 选择定时触发时展示 -->
          <div class="selectBox" v-if="item.trigger === 'timer'">
            <el-input size="small" class="inputWidth" placeholder="cron表达式" v-model="item.timer"
              clearable>
            </el-input>
          </div>

          <!-- 选择设备触发时展示 -->
          <div class="selectBox" v-if="item.trigger === 'device'">
            <!-- 选择设备 -->
            <div>
              <el-input size="small" placeholder="点击选择设备" :value="item.device.name"
                class="inputWidth">
                <el-button slot="append" icon="el-icon-printer" @click="selectFacility(index, '触发器')"></el-button>
              </el-input>
            </div>

            <!-- 选择触发类型 -->
            <el-select v-if="item.device.name" style="margin-top:15px;" class="inputWidth"
              size="small" v-model="item.device.messageType"
              @change="facilityTriggerChange($event, item,index)" placeholder="请选择触发类型">
              <el-option v-for="op in facilityTypeOptions" :key="op.value" :label="op.label"
                :value="op.value">
              </el-option>
            </el-select>

            <!-- 如果为属性/事件/功能的话就同行展示选择具体触发类型 -->
            <el-select v-if="
            item.device.messageType === 'properties' ||
            item.device.messageType === 'event' ||
            item.device.messageType === 'function'
                " style="margin-top:15px;" class="inputWidth marginLeft" size="small" v-model="
                  item.facilityMoreOption.moreOption.specificFacilityTypeValue
                " @change="specificTriggerOptionChange($event, item,index)" placeholder="请选择具体触发选项">
              <el-option v-for="op in item.facilityMoreOption.moreOption
                    .specificFacilityTypeOptions" :key="op.id" :label="op.name + '(' + op.sn + ')'" :value="op.id">
              </el-option>
            </el-select>
          </div>

          <!-- 选择设备触发时展示 -->
          <!-- 并且在具体选择类型中选择 属性/事件/功能 时展示-->
          <template v-if="
                item.triggerConditionValue === 'device' &&
                  ( item.device.messageType === 'properties' ||
                  item.device.messageType === 'event' ||
                  item.device.messageType === 'function')
              ">
            <div class="selectBox" v-for="(filte, filteIdx) in item.facilityMoreOption.moreOption
                  .filterConditionDataList" :key="filte.idx">
              <!-- 过滤条件KEY -->
              <el-select class="inputWidth" size="small" v-model="filte.filterConditionKeyValue" placeholder="过滤条件KEY"
                disabled>
                <el-option v-for="op in filte.filterConditionKeyOptions" :key="op.value" :label="op.label"
                  :value="op.value">
                </el-option>
              </el-select>
              <!-- 操作符 dataInfo.triggers[index].device.filters[0].operator-->
              <el-select class="inputWidth marginLeft" @change="operatorchange($event,index,filteIdx)" size="small"
                v-model="filte.operatorValue" placeholder="操作符">
                <el-option v-for="op in filte.operatorOptions" :key="op.value" :label="op.label" :value="op.value">
                </el-option>
              </el-select>

              <el-input class="inputWidth marginLeft" size="small" @input="filtersvalue($event,index,filteIdx)"
                v-model="filte.filterConditionValue" placeholder="过滤条件值"></el-input>

              <el-button class="marginLeft" type="text" @click="deleteFilterCondition(index, filteIdx)">
                删除
              </el-button>
            </div>

            <div class="addFilterCondition">
              <el-button type="text" icon="el-icon-plus" @click="addFilterCondition(index)">
                添加过滤条件
              </el-button>
            </div>
          </template>

          <!-- 选择场景触发时展示 -->
          <div class="selectBox" v-if="item.triggerConditionValue === 'scene'">
            <el-select multiple class="inputWidth" size="small" v-model="dataInfo.triggers[index].scenes"
              placeholder="请选择场景联动（可多选）">
              <el-option v-for="ops in scencOptionsData" :key="ops.id" :label="ops.name" :value="ops.id">
              </el-option>
            </el-select>
          </div>
        </div>

        <div class="addBox">
          <el-button type="text" icon="el-icon-plus" @click="addItem('triggers')">新增触发器</el-button>
        </div>
      </div>
    </div>

    <!-- 中间的选择 暂未开放切换 -->
    <div class="triggerConditionBox">
      <div class="text">当以上条件</div>
      <nav-tabs class="navTabs" :navTabs="navTabs" @navTabsChange="navTabsChange" :tabsStyle="tabsStyle"
        :defaultIdx="defaultIdx" :isAllowClick="false" />
      <div class="text">时，触发以下执行动作</div>
    </div>

    <!-- 执行动作 -->
    <div class="addedLinkage footer">
      <div class="triggerCondition" v-if="isExecute">
        <div class="text">还未添加执行动作</div>
        <div class="btn">
          <el-button size="small" type="info" icon="el-icon-plus" @click="isExecute = false">添加执行动作</el-button>
        </div>
      </div>

      <div class="itemMaxBox" v-else>
        <div class="titleBox">
          <span>执行动作</span>
          <el-switch :width="60" style="margin-left:30px;" v-model="dataInfo.parallel" active-text="串行执行"
            inactive-text="并行执行" active-value="0" inactive-value="1">
          </el-switch>
        </div>
        <div class="itemBox" v-for="(item, index) in actions" :key="item.idx">
          <div class="titleBox">
            <div class="titleText">{{ item.executeName + item.idx }}</div>
            <el-button type="text" @click="deleteItem(index, 'actions')">删除</el-button>
          </div>
          <div class="selectBox">
            <!-- <el-select class="inputWidth" size="small" v-model="item.executeValue" placeholder="请选择执行动作"> -->
            <el-select class="inputWidth" size="small" @change="executorchange($event,index)"
              v-model="dataInfo.actions[index].executor" placeholder="请选择执行动作">
              <el-option v-for="ops in item.executeOptions" :key="ops.value" :label="ops.label" :value="ops.value">
              </el-option>
            </el-select>

            <!-- 如果选择了消息通知   暂时未封装参数-->
            <template v-if="dataInfo.actions[index].executor === 'message-notifier'">
              <el-select class="inputWidth marginLeft" size="small" v-model="item.informMoreOption.informTypeValue"
                placeholder="请选择通知类型">
                <el-option v-for="ops in item.informMoreOption.informTypeOptions" :key="ops.value" :label="ops.label"
                  :value="ops.value">
                </el-option>
              </el-select>
              <el-select class="inputWidth marginLeft" size="small" v-model="item.informMoreOption.informConfigValue"
                placeholder="请选择通知配置">
                <el-option v-for="ops in item.informMoreOption.informConfigOptions" :key="ops.value" :label="ops.label"
                  :value="ops.value">
                </el-option>
              </el-select>
              <el-select class="inputWidth marginLeft" size="small" v-model="item.informMoreOption.informTempValue"
                placeholder="请选择通知模板">
                <el-option v-for="ops in item.informMoreOption.informTempOptions" :key="ops.value" :label="ops.label"
                  :value="ops.value">
                </el-option>
              </el-select>
            </template>

            <!-- 如果选择了设备输出 -->
            <template v-if="dataInfo.actions[index].executor === 'device-sender'">
              <el-input size="small" placeholder="点击选择设备" :value="item.facilityMoreOption.facilityInfo.name"
                class="inputWidth marginLeft">
                <el-button slot="append" icon="el-icon-printer" @click="selectFacility(index, '执行动作')"></el-button>
              </el-input>
              <!-- <el-select class="inputWidth marginLeft" size="small" v-model="item.facilityMoreOption.messageType" -->
              <el-select class="inputWidth marginLeft" size="small" @change="messageTypechange($event,item,index)"
                v-model="item.facilityMoreOption.messageType" placeholder="请选择动作类型">
                <el-option v-for="ops in item.facilityMoreOption.messageTypeOptions" :key="ops.value" :label="ops.label"
                  :value="ops.value">
                </el-option>
              </el-select>
              <el-select @change="actionsOptionChange($event, item,index)" clearable
                v-if="item.facilityMoreOption.messageType == 'SET_PROPERTIES'" class="inputWidth marginLeft"
                size="small" v-model="item.facilityMoreOption.inputs[0].pointId" placeholder="选择属性">
                <el-option v-for="ops in item.facilityMoreOption.facilityInfo.pointList" :key="ops.pointId"
                  :label="ops.name + '(' + ops.sn + ')'" :value="ops.pointId">
                </el-option>
              </el-select>
              <el-input v-if="item.facilityMoreOption.messageType == 'SET_PROPERTIES'"
                @input="inputchange($event,index)" class="inputWidth marginLeft" size="small"
                v-model="item.facilityMoreOption.inputs[0].value" placeholder="设置属性值"></el-input>

              <el-select v-if="item.facilityMoreOption.messageType == 'INVOKE_FUNCTION'" class="inputWidth marginLeft"
                size="small" v-model="item.facilityMoreOption.funcTypeValue" placeholder="请选择功能类型">
                <el-option v-for="ops in item.facilityMoreOption.funcTypeOptions" :key="ops.value" :label="ops.label"
                  :value="ops.value">
                </el-option>
              </el-select>
            </template>
          </div>
        </div>

        <div class="addBox">
          <el-button type="text" icon="el-icon-plus" @click="addItem('actions')">新增执行动作</el-button>
        </div>
      </div>
    </div>

    <binding-child-facility-dialog @selectChange="selectChangeHandle" ref="dialogRef" />
  </div>
</template>

<script>
  import navTabs from "@/components/navTabs/navTabs.vue";
  import BindingChildFacilityDialog from "./bindingChildFacilityDialog.vue";
  import { engineInstanceAdd, engineInstanceList, orgTree, xqSelectList } from "../../../../url/api.js"
  import {
    defaultTriggerConditionList,
    defaultExecuteList
  } from "../config/addedLinkageConfig";

  import {
    executeRecursion,
    triggerConditionRecursion
  } from "../hooks/useFieldIsEmpty";
  import { log } from "three";

  export default {
    components: { navTabs, BindingChildFacilityDialog },
    data() {
      return {
        //触发器下拉选项
        triggerOptionsScene: [//情景模式
          {
            label: "手动触发",
            value: 'manual'
          },
          {
            label: "定时触发",
            value: 'timer'
          },
        ],
        triggerOptionsLinkage: [//场景联动
          {
            label: "设备触发",
            value: 'device'
          },
          {
            label: "场景触发",
            value: 'scene'
          }
        ],
        facilityTypeOptions: [
        {
          label: "上线",
          value: 'online'
        },
        {
          label: "离线",
          value: 'offline'
        },
        {
          label: "属性",
          value: 'properties'
        },
        {
          label: "事件",
          value: 'event'
        },
        {
          label: "功能",
          value: 'function'
        }
      ], //设备触发选项列表
        xqData: '',
        option1: [],
        zuzhiTree: [],
        xqTree: [],
        defaultProps2: {
          children: 'children',
          label: 'name',
          value: 'id',
          expandTrigger: 'hover',
          checkStrictly: true,
        },
        // 页面相关
        navTabs: ["同时满足", "任意满足"],
        tabsStyle: {
          padding: "5px 25px",
          cursor: "not-allowed"
        },
        currentIdx: 0,
        defaultIdx: 1,
        isClickTrigger: false,
        isExecute: false,
        scencOptionsData: [],
        // 数据相关
        dataInfo: {
          name: "",
          description: "",
          parallel: false,
          triggers: [
            {
              device: {
                deviceId: "",
                filters: [
                  {
                    key: "",
                    operator: "",
                    ruleInstanceId: "",
                    value: ""
                  }
                ],
                messageType: "",
                modelId: "",
                productId: "",
                shakeLimit: {
                  alarmFirst: true,
                  enabled: true,
                  threshold: 0,
                  time: 0
                },
                "sn": ""
              },
            }
          ],//
          actions: [],
        },
        // 触发器下拉列表数组
        triggers: [],
        // 执行动作下拉列表数组
        actions: [],//执行动作
        triggerSelectFacilityIndex: -1, //临时存储点击选择绑定子设备(触发器)弹框列表时的索引 用于修改数据
        executeSelectFacilityIndex: -1, //临时存储点击选择绑定子设备(执行动作)弹框列表时的索引 用于修改数据

        defaultFilterCondition: {}, //默认的过滤条件  每当具体触发选项更改后赋值一次 保证再添加更多过滤选项时都是初始的值
        routequery: {},
      };
    },
    created() {
      this.initData();
      this.routequery = this.$route.query;
      console.log(this.routequery, "AAAAAAAAAA");
      this.dataInfo.modelType = this.routequery.modelType;//赋值 modelType
      // modelType
      // debugger
    },
    methods: {
      filtersvalue(e, index, filteIdx) {
        debugger
        this.dataInfo.triggers[index].device.filters[filteIdx].value = e;
        debugger
      },
      operatorchange(e, index, filteIdx) {
        this.dataInfo.triggers[index].device.filters[filteIdx].operator = e;
        console.log(this.dataInfo, "QQQQQQQQQQQQQQQ");

      },
      executorchange(data, index) {

        if (data == 'device-sender') {
          this.dataInfo.actions[index].deviceAction = {
            "deviceId": "",
            "inputs": [
              {
                "name": "",
                "pointId": "",
                "value": ""
              }
            ],
            "messageType": "",
            "productId": "",
            "sn": ""
          }
        };
        console.log(this.dataInfo, "AAAAAAAAAA");
      },
      checkedXq(val) {
        this.dataInfo.projectId = val.id;
        this.dataInfo.projectName = val.name;
        // this.$forceUpdate(); // 调用此函数方法
      },
      // 选择切换 目前不可切换
      navTabsChange(i) {
        this.currentIdx = i;
      },

      // 删除过滤条件
      deleteFilterCondition(index, filteIdx) {
        const dataObj = this.triggers[index].facilityMoreOption
          .moreOption.filterConditionDataList;

        if (dataObj.length > 1) {
          dataObj.splice(filteIdx, 1);
          this.dataInfo.triggers[index].device.filters.splice(filteIdx, 1);
          console.log(this.dataInfo, "删除过滤条件");
        } else {
          this.$message.warning("至少保留一个选择项");
        }
      },

      // 添加过滤条件
      addFilterCondition(index) {
        //获取原来的dataList
        const thisDataObj = this.triggers[index].facilityMoreOption
          .moreOption.filterConditionDataList;

        // 深拷贝后修改下idx值
        const obj = JSON.parse(JSON.stringify(this.defaultFilterCondition));
        obj.idx = thisDataObj[thisDataObj.length - 1].idx + 1;

        thisDataObj.push(obj);
        let key = this.dataInfo.triggers[index].device.filters[0].key
        this.dataInfo.triggers[index].device.filters.push({ key: key });
        console.log(this.dataInfo, "AASSASASASAS");
      },

      // 删除item
      deleteItem(index, field) {
        if (this[field].length > 1) {
          this[field].splice(index, 1);
          // this.dataInfo.triggers.splice(index, 1);
        } else {
          this.$message.warning("至少保留一个选择项");
        }
      },

      // 添加item
      addItem(field) {
        let obj = JSON.parse(JSON.stringify(defaultTriggerConditionList))[0];
        if (field === "actions") {
          obj = JSON.parse(JSON.stringify(defaultExecuteList))[0];
          this.dataInfo.actions.push({})
        } else {
          this.dataInfo.triggers.push({})
        }
        console.log(this.triggers, "AAAAAAAAAAAAAAAAA");
        // 取最后一项的idx+1
        obj.idx = this[field][this[field].length - 1].idx + 1;
        this[field].push(obj);


        console.log(this.actions);
      },

      // 选择设备按钮点击事件 给子组件传入type
      selectFacility(index, type) {
        if (type === "触发器") {
          this.triggerSelectFacilityIndex = index;
        } else {//执行动作
          this.executeSelectFacilityIndex = index;
        }
        this.$refs.dialogRef.dialog.show = true;
        this.$refs.dialogRef.whoClick = type;
      },

      // 绑定子设备的事件处理
      selectChangeHandle(data) {//勾选的设备数据
        let thisData = null;
        debugger
        console.log(data,"勾选的数据");
        if (data.whoClick === "触发器") {
          const resetData = this.triggers[
            this.triggerSelectFacilityIndex
          ];
          const val = JSON.parse(
            JSON.stringify(defaultTriggerConditionList[0].facilityMoreOption)
          );
          this.$set(resetData, "facilityMoreOption", val);

          thisData = resetData.facilityMoreOption;
          this.$set(thisData, "facilityInfo", data);
          if (!this.dataInfo.triggers[this.triggerSelectFacilityIndex].device) {
            this.dataInfo.triggers[this.triggerSelectFacilityIndex].device = {};
          }
          this.$set(this.dataInfo.triggers[this.triggerSelectFacilityIndex].device, "name", data.name);
          // this.dataInfo.triggers[this.triggerSelectFacilityIndex].device.name  = data.name;
          this.dataInfo.triggers[this.triggerSelectFacilityIndex].device.deviceId = data.id;//赋值deviceId
          this.dataInfo.triggers[this.triggerSelectFacilityIndex].device.productId = data.productId;//赋值productId
        } else {//执行动作
          // const executeData = this.actions[
          //   this.executeSelectFacilityIndex
          // ];
          // const val = JSON.parse(
          //   JSON.stringify(defaultTriggerConditionList[0].facilityMoreOption)
          // );

          thisData = this.actions[this.executeSelectFacilityIndex].facilityMoreOption;
          this.$set(thisData, "facilityInfo", data);
          this.dataInfo.triggers[this.executeSelectFacilityIndex].device.name  = data.name;
          this.dataInfo.actions[this.executeSelectFacilityIndex].deviceAction.deviceId = data.id;//赋值deviceId
          this.dataInfo.actions[this.executeSelectFacilityIndex].deviceAction.deviceId = data.productId;

        }

        // this.$set(thisData, "facilityInfo", data);
        // console.log(this.actions[this.executeSelectFacilityIndex].facilityMoreOption);
      },

      // 设备触发 - 选择触发类型时候的监听函数 动态改变具体触发选项(specificFacilityTypeOptions)的options
      facilityTriggerChange(e, item, index) {
        debugger
        const facilityInfo = item.facilityMoreOption.facilityInfo;

        const moreOption = item.facilityMoreOption.moreOption;
        this.resetFilterCondition(item);

        const maps = {
          3: "pointList",
          4: "eventList",
          5: "functionList"
        };
        const list = facilityInfo[maps[e]];

        this.$set(moreOption, "specificFacilityTypeValue", "");
        this.$set(moreOption, "specificFacilityTypeOptions", list);
        if (!this.dataInfo.triggers[index].device) {
          this.dataInfo.triggers[index].device = {}
        }
        if (!this.dataInfo.triggers[index].device.filters) {
          this.dataInfo.triggers[index].device.filters = [{}];
        }
        // if (e == 1) {
        //   this.dataInfo.triggers[index].device.messageType = "online"
        // } else if (e == 2) {
        //   this.dataInfo.triggers[index].device.messageType = "offline"
        // } else if (e == 3) {
        //   this.dataInfo.triggers[index].device.messageType = "properties"
        // } else if (e == 4) {
        //   this.dataInfo.triggers[index].device.messageType = "function"
        // } else if (e == 5) {
        //   this.dataInfo.triggers[index].device.messageType = "event"
        // }

      },

      // 设备触发 - 改变具体触发选项(specificFacilityTypeOptions)后的监听函数 再此获取过滤条件的KEY
      specificTriggerOptionChange(e, item, index) {
        debugger
        if (!this.dataInfo.triggers[index].device) {
          this.dataInfo.triggers[index].device = {};
        }
        if (!this.dataInfo.triggers[index].device.filters) {
          this.dataInfo.triggers[index].device.filters[0] = {};
        }
        this.dataInfo.triggers[index].device.modelId = e;//赋值

        console.log(this.dataInfo, "AAAAAAAAAAAAA");
        this.resetFilterCondition(item);
        const obj = item.facilityMoreOption.moreOption.specificFacilityTypeOptions.find(
          d => d.id === e
        );
        const keyData =
          item.facilityMoreOption.moreOption.filterConditionDataList[0];
        this.$set(keyData, "filterConditionKeyOptions", {
          label: obj.sn,
          value: obj.sn
        });
        this.dataInfo.triggers[index].device.filters[0].key = obj.sn;//赋值key
        this.$set(keyData, "filterConditionKeyValue", obj.sn);
        this.defaultFilterCondition = JSON.parse(JSON.stringify(keyData));

      },

      actionsOptionChange(e, item, index) {
        //  this.actions[index].facilityMoreOption.inputs[0].name = item.
        //  this.actions[index].facilityMoreOption.inputs[0].pointId = e.pointId
        const obj = item.facilityMoreOption.facilityInfo.pointList.find(
          d => d.id === e
        );
        console.log(e, item, index, "QQQQQQQQQQQQQQQQQQ");
        if (!this.dataInfo.actions[index].deviceAction.inputs) {
          this.dataInfo.actions[index].deviceAction.inputs = [
            {
              name: '',
              pointId: '',
              value: '',
            }
          ]
        }
        this.dataInfo.actions[index].deviceAction.inputs[0].pointId = e;//赋值
      },
      messageTypechange(e, item, index) {
        this.dataInfo.actions[index].deviceAction.messageType = e;//赋值
        console.log(this.dataInfo.actions[index].deviceAction.messageType);
      },
      inputchange(value, index) {
        console.log(value, this.actions, "Aaaaaaaaaaaaaaa");
        this.dataInfo.actions[index].deviceAction.inputs[0].value = value;//赋值
      },

      // 重置过滤选项
      resetFilterCondition(item) {
        debugger
        const moreOption = item.facilityMoreOption.moreOption;

        const defaultObj =
          defaultTriggerConditionList[0].facilityMoreOption.moreOption
            .filterConditionDataList;

        const defaultVal = JSON.parse(JSON.stringify(defaultObj));

        this.$set(moreOption, "filterConditionDataList", defaultVal);
      },

      // 点击重置
      reset() {
        this.$confirm("是否确定要重置当前输入的所有数据?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.initData();
          })
          .catch(() => { });
      },

      // 确定添加事件
      saveAdd() {
        if (this.isEmpty()) {
          console.log(this.dataInfo, this.triggers, this.actions, "QWQWQWQWQWQWWQWWQWQQWQWQW");
          let params = this.dataInfo
          engineInstanceAdd(params).then((res) => {//新增
            console.log(res)
            if (res.data.ok) {
              // this.scencOptionsData = res.data.data
              this.$message.success("新增成功!");
            } else {
              this.$message.error(res.data.message);
            }
          })

          console.log(this.triggers);
          console.log(this.actions);
        }
      },

      // 确认添加时判断是否已经输入完全
      isEmpty() {
        let falg = false;
        if (!this.dataInfo.name) {
          this.$message.warning("请填写策略名称");
        } else if (!this.dataInfo.description) {
          this.$message.warning("请填写备注");
        }
        // else if (
        //   triggerConditionRecursion(
        //     this.triggers,
        //     "triggers"
        //   )
        // ) {
        //   this.$message.warning("请将所有触发器的选项填写完整，不可留空");
        // } else if (executeRecursion(this.actions, "actions")) {
        //   this.$message.warning("请将所有执行动作的选项填写完整，不可留空");
        // } 
        else {
          falg = true;
        }

        return falg;
      },
      zuzhiChange(value) {
        //改变组织
        if (value.length != 0) {
          console.log(value);
          // this.dataInfo.orgId =
          //   this.$refs.cascader.getCheckedNodes()[0].data.id;
        } else {
          // this.dataInfo.orgId = '';
        }
        this.getproject();
        console.log(this.option1);

      },
      getproject() {
        let org_tree1 = {
          orgId: this.dataInfo.orgId
        };
        xqSelectList(org_tree1).then((res) => {
          //项目选择列表
          if (res.data.ok) {
            this.xqTree = res.data.data;
            console.log(this.xqTree);
            if (this.xqTree.length != 0) {
              this.dataInfo.projectId = this.xqTree[0].id;
              this.dataInfo.projectName = this.xqTree[0].name;
            }
          }
        });
      },
      getengineInstanceList() {
        let params = {
          filterScene: true,
          orgId: this.dataInfo.orgId,
        }
        engineInstanceList(params).then((res) => {//查询系统所有的菜单
          console.log(res)
          if (res.data.ok) {
            this.scencOptionsData = res.data.data
          }
        })
      },
      // 初始化数据
      initData() {
        this.triggers = JSON.parse(
          JSON.stringify(defaultTriggerConditionList)
        );

        // this.dataInfo.triggers.push({}),
        //   this.dataInfo.actions.push({}),
        //   this.actions = JSON.parse(JSON.stringify(defaultExecuteList));
        let org_tree = {
          name: '',
          status: 1,
        };
        orgTree(org_tree).then((res) => {
          //获取组织列表
          console.log(res);
          if (res.data.ok) {
            this.zuzhiTree = res.data.data;
            this.dataInfo.orgId = res.data.data[0].id;
            this.option1 = res.data.data[0].id;
            this.getproject();
            this.getengineInstanceList();
          }
        });
        // let org_tree1 = {
        //   orgId:this.dataInfo.orgId
        // };
        // xqSelectList(org_tree1).then((res) => {
        //     //项目选择列表
        //     if (res.data.ok) {
        //         this.xqTree = res.data.data;
        //         console.log(this.xqTree);
        //         if (this.xqTree.length != 0) {
        //             this.dataInfo.projectId = this.xqTree[0].id;
        //             this.dataInfo.projectName = this.xqTree[0].name;
        //         }
        //     }
        // });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .container {
    margin-right: 10px;
    font-size: 14px;

    .titleTr {
      margin: 20px 0px;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .inputBox {
        padding-left: 30px;

        .label {
          white-space: nowrap;
          font-size: 14px;
          position: relative;

          &:after {
            content: "*";
            color: #f56c6c;
            position: absolute;
            margin-left: -15px;
            font-size: 15px;
            top: 0;
            left: 0;
          }
        }

        .inputWidth {
          width: 260px;
        }

        display: flex;
        align-items: center;
      }
    }

    .addedLinkage {
      background-color: #fff;
      border-radius: 5px;
      padding: 15px 30px;

      .triggerCondition {
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        min-height: 300px;

        .text {
          color: rgba(16, 16, 16, 73);
          font-size: 28px;
          font-family: SourceHanSansSC-regular;
        }

        .btn {
          margin-top: 10px;

          /deep/ .el-button--info {
            color: #333;
            background-color: #dbdbdb;
            border-color: #dbdbdb;
          }
        }
      }

      .itemMaxBox {
        padding: 20px 0;
        color: #666;

        .titleBox {
          font-size: 18px;
          display: flex;
          align-items: center;
          margin-bottom: 15px;

          .icon {
            margin-left: 6px;
            width: 20px;
            cursor: pointer;
          }
        }

        .itemBox {
          background-color: #f5f5f6;
          padding: 5px 15px;
          border-radius: 5px;
          padding-bottom: 20px;
          margin-bottom: 10px;

          .titleBox {
            .titleText {
              width: 283px;
            }

            font-size: 13px;
            margin-bottom: 10px;
          }

          .selectBox {
            margin-bottom: 15px;
            margin-left: 15px;

            .debounce {
              display: inline-block;
              font-size: 14px;

              .text {
                margin-left: 30px;
              }
            }
          }

          .marginLeft {
            margin-left: 30px;
          }

          .inputWidth {
            width: 260px;
            position: relative;

            &:after {
              content: "*";
              color: #f56c6c;
              position: absolute;
              margin-left: -15px;
              font-size: 15px;
              top: 0;
              left: 0;
            }
          }
        }
      }
    }

    .triggerConditionBox {
      display: flex;
      align-items: center;
      justify-content: center;

      .navTabs {
        margin: 15px;
      }
    }

    .footer {
      margin-bottom: 50px;
    }
  }

  /deep/ .el-button--mini,
  .el-button--mini.is-round {
    padding: 5px 15px;
  }

  /deep/ .el-input__inner {
    max-width: 260px;
  }

  /deep/ .el-input {
    width: auto;
  }
</style>